package com.eerl.pdev.werl;

import com.eerl.pdev.werl.impl.ErlangNode;
import com.ericsson.otp.erlang.OtpErlangTuple;

public interface ERpcReturn<T> {

    /**
     * 是否正常
     */
    boolean getRetFlag();

    /**
     * 获得返回的匹配处理值
     */
    String getCaseMatch();

    /**
     * 错误发生时的信息
     */
    String getErrMsg();

    /**
     * 执行rpc后获得的数据
     */
    T getData();

    static String eRpcReturnTemplate(String handlerCaseMatch, boolean success, String secCaseMatch, String errMsg, String data) {
        return "{\"%s\", %s, \"%s\", %s, %s}".formatted(handlerCaseMatch, success, secCaseMatch, errMsg, data);
    }

    static String eRpcReturnTemplate(String handlerCaseMatch, boolean success, String errMsg, String data) {
        return eRpcReturnTemplate(handlerCaseMatch, success, "null", errMsg, data);
    }

    static String eRpcCallReturnTemplate(boolean success, String secCaseMatch, String errMsg, String data) {
        return eRpcReturnTemplate(ErlangNode.CALL_RPC_FLAG, success, secCaseMatch, errMsg, data);
    }
    static String eRpcCallReturnTemplate(boolean success, String errMsg, String data) {
        return eRpcCallReturnTemplate(success, "null", errMsg, data);
    }

    static boolean isSuccess(OtpErlangTuple ret) {
        return "true".equals(ret.elementAt(IS_SUCCESS_POS).toString());
    }

    public static final int HANDLE_CASE_MATCH_POS = 0;
    public static final int IS_SUCCESS_POS = 1;
    public static final int SEC_CASE_MATCH_POS = 2;
    public static final int ERR_MSG_POS = 3;
    public static final int DATA_POS = 4;

}
